package com.totofgeek.contactsboard.widget.outils;

import java.util.ArrayList;
import java.util.List;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import com.totofgeek.contactsboard.R;
import com.totofgeek.contactsboard.bean.Contact;
import com.totofgeek.contactsboard.outils.utilitaires.ContactUtilitaire;

/**
 * @author Totof
 * 
 */
class ListeRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory
{
	@SuppressWarnings("unused")
	private int				mAppWidgetId;

	private List<Contact>	listeContact	= new ArrayList<Contact>();
	private Context			mContext;

	/**
	 * @param context
	 * @param intent
	 */
	public ListeRemoteViewsFactory(Context context, Intent intent)
	{
		mContext = context;
		mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.RemoteViewsService.RemoteViewsFactory#onCreate()
	 */
	public void onCreate()
	{
		// In onCreate() you set up any connections / cursors to your data source. Heavy lifting,
		// for example downloading or creating content etc, should be deferred to onDataSetChanged()
		// or getViewAt(). Taking more than 20 seconds in this call will result in an ANR.
		ContactUtilitaire.open(mContext);
		listeContact = ContactUtilitaire.chercherListeContactsSelectionnes(mContext);
		ContactUtilitaire.fermerBaseDeDonnees();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.RemoteViewsService.RemoteViewsFactory#onDestroy()
	 */
	public void onDestroy()
	{
		listeContact.clear();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.RemoteViewsService.RemoteViewsFactory#getCount()
	 */
	public int getCount()
	{
		return listeContact.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.RemoteViewsService.RemoteViewsFactory#getViewAt(int)
	 */
	public RemoteViews getViewAt(int position)
	{
		RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);

		Contact contact = listeContact.get(position);

		rv.setImageViewBitmap(R.id.quick_contact_badge, contact.getPhoto());
		rv.setTextViewText(R.id.item_nom, contact.getNom());

		// Next, set a fill-intent, which will be used to fill in the pending intent template
		// that is set on the collection view in StackWidgetProvider.
		Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, contact.getCleUri());
		Intent fillInIntent = new Intent();
		fillInIntent.setData(uri);

		// Make it possible to distinguish the individual on-click
		// action of a given item
		rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);

		return rv;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.RemoteViewsService.RemoteViewsFactory#getLoadingView()
	 */
	public RemoteViews getLoadingView()
	{
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.RemoteViewsService.RemoteViewsFactory#getViewTypeCount()
	 */
	public int getViewTypeCount()
	{
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.RemoteViewsService.RemoteViewsFactory#getItemId(int)
	 */
	public long getItemId(int position)
	{
		return position;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.RemoteViewsService.RemoteViewsFactory#hasStableIds()
	 */
	public boolean hasStableIds()
	{
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.RemoteViewsService.RemoteViewsFactory#onDataSetChanged()
	 */
	public void onDataSetChanged()
	{
		ContactUtilitaire.open(mContext);
		listeContact = ContactUtilitaire.chercherListeContactsSelectionnes(mContext);
		ContactUtilitaire.fermerBaseDeDonnees();
	}
}
